// 生成随机验证码
function generateVerifyCode() {
  const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  let code = '';
  for (let i = 0; i < 4; i++) {
    const randomIndex = Math.floor(Math.random() * chars.length);
    code += chars[randomIndex];
  }
  return code;
}

// 显示验证码
function displayVerifyCode() {
  const code = generateVerifyCode();
  document.getElementById('codeDisplay').textContent = code;
}

// 刷新验证码按钮点击事件
document.getElementById('refreshCode').addEventListener('click', displayVerifyCode);

// 页面加载时生成并显示验证码
displayVerifyCode();

// 月薪滑块值显示
const salaryInput = document.getElementById('salary');
const salaryValue = document.getElementById('salaryValue');
salaryInput.addEventListener('input', function () {
  salaryValue.textContent = this.value;
});

// 照片预览
function previewPhoto(input) {
  if (input.files && input.files[0]) {
    const reader = new FileReader();
    reader.onload = function (e) {
      const preview = document.getElementById('photoPreview');
      preview.innerHTML = `<img src="${e.target.result}" alt="预览照片">`;
      preview.style.display = 'block';
    };
    reader.readAsDataURL(input.files[0]);
  }
}

// 表单提交验证
document.getElementById('registerForm').addEventListener('submit', function (e) {
  const phone = document.getElementById('phone').value;
  const password = document.getElementById('password').value;
  const email = document.getElementById('email').value;
  const verifyCodeInput = document.getElementById('verifyCode').value;
  const codeDisplay = document.getElementById('codeDisplay').textContent;

  if (!phone) {
    alert('手机号码不能为空');
    e.preventDefault();
    return;
  }
  if (phone.length !== 11) {
    alert('请输入11位手机号码');
    e.preventDefault();
    return;
  }
  if (!password) {
    alert('密码不能为空');
    e.preventDefault();
    return;
  }
  if (password.length !== 8) {
    alert('请输入8位密码');
    e.preventDefault();
    return;
  }
  if (!email) {
    alert('邮箱不能为空');
    e.preventDefault();
    return;
  }
  if (!verifyCodeInput) {
    alert('验证码不能为空');
    e.preventDefault();
    return;
  }
  if (verifyCodeInput !== codeDisplay) {
    alert('验证码错误');
    e.preventDefault();
    return;
  }
});